package com.cn.ServiceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cn.Constant.RedisConstant;
import com.cn.Domain.Setmeal;
import com.cn.Entity.PageResult;
import com.cn.Mapper.SetMealAndCheckGroupMapper;
import com.cn.Mapper.SetmealMapper;
import com.cn.Service.SetMealService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
@Autowired
    private SetmealMapper setmealMapper;
@Autowired
private JedisPool jedisPool;
@Autowired
private SetMealAndCheckGroupMapper setMealAndCheckGroupMapper;
     @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealMapper.insertSelective(setmeal);
        Integer setmealId = setmealMapper.selectPrimaryKey();
         setSetmealAndCheckGroup(setmealId,checkgroupIds);
         String img = setmeal.getImg();
         jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
     }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> setmeals = setmealMapper.selectByCondition(queryString);
         return new PageResult(setmeals.getTotal(),setmeals.getResult());
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Integer> findGroupIdsBySetMealId(Integer id) {
        return setMealAndCheckGroupMapper.findGroupIdsBySetMealId(id);
    }

    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
         String setmealImg = setmeal.getImg();
         setmealMapper.updateByPrimaryKeySelective(setmeal);
         Integer setmealId =setmeal.getId();
         setMealAndCheckGroupMapper.deleteByPrimaryKey(setmealId);
         setSetmealAndCheckGroup(setmealId,checkgroupIds);
         if (setmealImg!=null) {
             String img = setmeal.getImg();
             jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, img);
         }
    }

    @Override
    public String delete(Integer id) {
         String Img = setmealMapper.selectImg(id);
         setMealAndCheckGroupMapper.deleteByPrimaryKey(id);
         setmealMapper.deleteByPrimaryKey(id);
         return Img;
    }

    public void setSetmealAndCheckGroup(Integer setmealId,Integer[] checkgroupIds){
        Map<String,Integer> map = new HashMap<>();
        if (checkgroupIds!=null&&checkgroupIds.length>0){
            for (Integer checkgroupId : checkgroupIds) {
                map.put("SetMealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setMealAndCheckGroupMapper.setSetmealAndCheckGroup(map);
            }
        }
    }
}
